package com.eidos.blogs.common.rest;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.List;
import java.util.Objects;

/**
 * @author ：Eidos
 * @date ：Created in 2020/12/15
 * @description：数据网格返回类型封装
 * @modified By：
 * @version: 1.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class DataTableResponse<T> {


    /**
     * 总记录数
     */
    @ApiModelProperty("总记录数")
    private Long recordsTotal;
    /**
     * 当前记录数
     */
    @ApiModelProperty("当前记录数")
    private Long recordsFiltered;
    /**
     * 当前页大小
     */
    @ApiModelProperty("当前页大小")
    private Long pageSize;
    /**
     * 当前记录数
     */
    @ApiModelProperty("当前记录数")
    private Long size;
    /**
     * datatable必要参数
     */
    @ApiModelProperty("datatable必要参数,请求中携带的")
    private Object draw;
    /**
     * datatable必要参数
     */
    @ApiModelProperty("datatable必要参数，类似msg")
    private Object resultData;
    @ApiModelProperty("返回数据集")
    private List<T> data;

    private DataTableResponse(Object draw, Object resultData, IPage<T> page) {
        if (!Objects.equals(null, page)) {
            this.recordsTotal = page.getTotal();
            this.recordsFiltered = page.getTotal();
            this.pageSize = page.getPages();
            this.size = page.getSize();
            this.data = page.getRecords();

        }
        if (!Objects.equals(null, resultData)) {
            this.resultData = resultData;
        }
        this.draw = draw;
    }


    public static <T> DataTableResponse<T> success(IPage<T> page, Object draw) {
        return new DataTableResponse<>(draw, null, page);
    }

    public static <T> DataTableResponse<T> success(Object draw) {
        return new DataTableResponse<>(draw, null, new Page<>());
    }

    public static <T> DataTableResponse<T> failed(String msg, Object draw) {
        return new DataTableResponse<>(draw, msg, null);
    }

    public static <T> DataTableResponse<T> failed(Object draw) {
        return new DataTableResponse<>(draw, HttpCodeEnum.FAIL_CODE.getMsg(), null);
    }

    public static <T> DataTableResponse<T> failed(HttpCodeEnum httpCodeEnum, Object draw) {
        return new DataTableResponse<>(draw, httpCodeEnum.getMsg(), null);
    }
}
